package com.yycx.module.bus.provider.controller;

import cn.hutool.core.map.MapUtil;
import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.mybatis.model.ResultBody;
import com.yycx.common.security.OpenHelper;
import com.yycx.common.security.OpenUser;
import com.yycx.common.springmvc.base.BaseController;
import com.yycx.common.utils.StringUtils;
import com.yycx.module.bus.client.entity.BusDrugs;
import com.yycx.module.bus.client.entity.hy.BusPolicyDrug;
import com.yycx.module.bus.provider.service.BusDrugsService;
import com.yycx.module.bus.provider.service.hy.BusPolicyDrugService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;


/**
 * 药品控制器
 *
 * @author flyme
 * @date 2020-07-26
 */
@RestController
@RequestMapping("/bus/drugs/")
public class BusDrugsController extends BaseController<BusDrugsService, BusDrugs> {

    @Autowired
    private BusPolicyDrugService busPolicyDrugService;

    @ApiOperation(value = "药品-分页列表", notes = "药品分页列表")
    @GetMapping(value = "page")
    public ResultBody pageList(@RequestParam(required = false) Map params) {
        return bizService.pageList(params);
    }

    @ApiOperation(value = "药品-列表", notes = "药品列表")
    @GetMapping(value = "list")
    public ResultBody list(@RequestParam(required = false) Map params) {
        return bizService.listEntityMap(params);
    }

    @ApiOperation(value = "药品-添加", notes = "添加药品")
    @PostMapping(value = "save")
    public ResultBody save(@RequestParam(required = false) Map params) {
        return bizService.add(params);
    }

    @ApiOperation(value = "药品-更新", notes = "更新药品")
    @PostMapping(value = "update")
    public ResultBody update(@RequestParam(required = false) Map params) {
        return bizService.edit(params);
    }

    @ApiOperation(value = "药品-删除", notes = "删除药品")
    @PostMapping(value = "remove")
    public ResultBody remove(@RequestParam(required = false) Map params) {
        return bizService.delete(params);
    }


    @ApiOperation(value = "药品-详情", notes = "药品详情")
    @GetMapping(value = "get")
    public ResultBody get(@RequestParam(required = false) Map params) {
        return bizService.get(params);
    }

    @ApiOperation(value = "药品-详情", notes = "药品详情")
    @GetMapping(value = "get2")
    public ResultBody get2(@RequestParam(required = false) Map params) {
        return bizService.get(params);
    }


    @ApiOperation(value = "获取可关联商品列表")
    @GetMapping(value = "getAuthDrugList")
    public ResultBody getAuthDataScopeList(@RequestParam(value = "policyDrugId") Long policyDrugId) {
        EntityMap map = new EntityMap();
        List<Long> drugIds = bizService.getAuthList(policyDrugId);
        BusPolicyDrug policyDrug = busPolicyDrugService.getById(policyDrugId);
        //全部字段
        List<EntityMap> drugList = bizService.selectLikeByDrugNameAndSpec(policyDrug.getDrugsName(), policyDrug.getDrugsSpec());
        map.put("drugIds", drugIds);
        map.put("drugList", drugList);
        return ResultBody.ok(map);
    }


    /**
     * 设置商品政策
     *
     * @param policyDrugId
     * @param drugIds
     * @return
     */
    @ApiOperation(value = " 设置商品政策", notes = " 设置商品政策")
    @PostMapping("setAuthDrugs")
    public ResultBody setAuthDataScope(@RequestParam(value = "policyDrugId") Long policyDrugId, @RequestParam(value = "drugIds", required = false) String drugIds) {
        bizService.saveAuthDrugs(policyDrugId, StringUtils.isNotBlank(drugIds) ? drugIds.split(",") : new String[]{});
        return ResultBody.msg("设置成功");
    }

    @ApiOperation(value = "更新零售价", notes = "更新零售价")
    @PostMapping(value = "importData")
    public ResultBody importData(@RequestParam(required = false) Map params, @RequestParam("file") MultipartFile file) {
        ResultBody resultBody = bizService.importData(file, params);
        return resultBody;
    }

    /**
     * 导出数据
     *
     * @param params
     * @param request
     * @param response
     */
    @PostMapping(value = "export")
    public void export(@RequestParam(required = false) Map params, HttpServletRequest request, HttpServletResponse response) {
        String handlerName = MapUtil.getStr(params, "handlerName");
        String fileName = MapUtil.getStr(params, "fileName");
        bizService.export(params, request, response, fileName, fileName, handlerName);
    }

    @ApiOperation(value = "同步海鹰商品", notes = "同步海鹰商品")
    @PostMapping(value = "syncHyDrug")
    public ResultBody syncHyDrug() {
        OpenUser openUser = OpenHelper.getUser();
        bizService.syncHyDrug(openUser);
        return ResultBody.msg("同步完成,请稍后刷新");
    }
}
